import VueRouter from 'vue-router'

import Vue from 'vue'
//工具
import auth from '@/utils/auth'
//导入组件
//疑问：index.js index.vue index.json  优先级和我书写的顺序一致
import login from '@/views/login'
import Home from '@/views/home'
import Welcome from '@/views/welcome'
import NotFound from '@/views/404'
import Article from '@/views/article'
import Image from '@/views/image'
import Publish from '@/views/publish'
import Comment from '@/views/comment'
import Fans from '@/views/fans'

Vue.use(VueRouter)
//初始化
const router = new VueRouter({
    routes: [
        {path:'/login', component: login},
        {
          path: '/',
          component: Home,
          children: [
            { path: '/', component: Welcome },
            { path:'/article', component:Article },
            { path:'/image', component:Image },
            { path: '/publish', component: Publish },
            { path: '/comment', component: Comment },
            { path: '/fans', component: Fans }
          ]
        },
       // 通配规则（以上所有的规则不符合的时候，走这个规则）
       {path:'*',component: NotFound }
      ]
  })
//前置导航
router.beforeEach((to, from, next) => {
  //除去/login路由跳转，都需要本地存储了token,才能跳转
  //若不是登录页面并且也没有token,拦截到登录页面
  if (to.path !== '/login' && !auth.getUser().token) return next('/login')
  //其他情况放行
  next()
  
})


export default router